package com.twu.biblioteca;

import java.util.ArrayList;

public class Library {
    private ArrayList<Item> itemArrayList = new ArrayList<Item>();
    public Library() {

        itemArrayList.add(new Movie("title1", "director1", 3, 2000, 1));
        itemArrayList.add(new Movie("title2", "director2", 7, 2001, 2));
        itemArrayList.add(new Movie("title3", "director", 8, 2000, 3));
        itemArrayList.add(new Book("title4",4));
        itemArrayList.add(new Book("title5",5));
    }
    public void addItemArrayList(Item item){
        itemArrayList.add(item);
    }
    public Item getByid(int id) {
        for(Item b: itemArrayList){
            if(b.getId()==id)
                return b;
        }
        return null;
    }

    public ArrayList<Item> getList(){
        return itemArrayList;

    }
    public boolean returnOfItem(int id) {
        Item b1 = getByid(id);
        if(b1==null)
            System.out.println("there is no item with given id");
        else if(b1.getClass()==Book.class)
            System.out.println("That is not a valid book to return");
        else if(b1.getClass()==Movie.class && b1.getNumberOfAvailableCopies()>1)
            System.out.println("That is not a valid Movie to return");
        else {
            if(b1.resetChecked()) {
                System.out.println("Thank you for returning the item");
                return true;
            }
        }
        return false;
    }

    public boolean checkedOut(int itemId) {
        Item b = getByid(itemId);
        if(b!=null && (b.getNumberOfAvailableCopies())<=0){
            System.out.println("not available");
        }
        else if(b!=null){
            if(b.setChecked()) {
                System.out.println("Thank you! Enjoy the movie”.");
                return true;
            }
        }
        else
            System.out.println("There is no item with id = "+itemId);
        return false;
    }

    public ArrayList<Movie> getListofMovies() {
        ArrayList<Item> moviesList =getList();
        ArrayList<Movie> answer = new ArrayList<Movie>();
        for(Item i : moviesList){
            if(i.getClass()==Movie.class && (i.getNumberOfAvailableCopies())>0)
                answer.add((Movie)i);
        }
        return answer;
    }
    public ArrayList<Book> getListOfBooks() {
        ArrayList<Item> BookList =getList();
        ArrayList<Book> answer = new ArrayList<Book>();
        for(Item i : BookList){
            if(i.getClass()==Book.class && (i.getNumberOfAvailableCopies()>0))
                answer.add((Book)i);
        }
        return answer;
    }
}
